package nl.ruudshuis.data;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.ruudshuis.beans.GebruikerBean;
import nl.ruudshuis.dao.BiedingDao;
import nl.ruudshuis.db.DBmanager;

/**
 * Servlet implementation class BiedingController
 */
public class BiedingController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BiedingController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String vID = request.getParameter("veilingId");
		BiedingDao dao = new BiedingDao((DBmanager) getServletContext().getAttribute("dbman"));

		int veilingId = -1;
		if (vID != "" || vID != null) {
			veilingId = Integer.parseInt(vID);
		}
		String b = request.getParameter("bedrag");
		double bod = 0.0;
		if (b != "") {
			bod = Double.parseDouble(b);
		}
		
		double prijs = dao.getLaatsteBod(veilingId);

		HttpSession session = request.getSession();
		int user = 0;
		if(session.getAttribute("loggedIn") != null){
			if(bod > prijs){
				GebruikerBean gb = (GebruikerBean) session.getAttribute("loggedIn");
				user = gb.getId();
				dao.createBieding(veilingId, user, bod);
				System.out.println(dao.getBiedingen(veilingId));

				response.sendRedirect("VeilingController?item="+veilingId);
			}
			else{
				response.sendRedirect("VeilingController");
			}
		}
		else{
			response.sendRedirect("VeilingController");
		}
	}

}
